/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.vanniktech.emoji.sample;

import com.vanniktech.emoji.EmojiconText;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.app.Context;

import java.util.List;

/**
 * TextDialogProvider
 *
 * @since 2021-06-28
 */
public class TextDialogProvider extends BaseItemProvider {
    private Context mContext;
    private List<String> mStringList;

    /**
     * TextDialogProvider
     *
     * @param context 上下文
     * @param stringList 数据集合
     */
    public TextDialogProvider(Context context, List<String> stringList) {
        mContext = context;
        mStringList = stringList;
    }

    @Override
    public int getCount() {
        return mStringList.size();
    }

    @Override
    public Object getItem(int i) {
        return mStringList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        Component cpt;
        if (component == null) {
            cpt = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_item_dialog_text, null, false);
        } else {
            cpt = component;
        }
        EmojiconText emojiconText = ((EmojiconText) cpt.findComponentById(ResourceTable.Id_item_str));
        if (EmojiUtil.onlyEmoji(mStringList.get(i))) {
            emojiconText.setTextSize(120);
        } else if (EmojiUtil.containsEmoji(mStringList.get(i)) && !EmojiUtil.containsChar(mStringList.get(i))) {
            emojiconText.setTextSize(80);
        } else {
            emojiconText.setTextSize(35);
        }
        emojiconText.setText(mStringList.get(i));
        return cpt;
    }
}
